package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.entity.User;
import com.zzyl.mapper.NursingPlanMapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.mapper.UserMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 护理计划表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-15
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements NursingPlanService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private NursingPlan nursingPlan;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Override
    public PageResponse<NursingPlan>findByIdPage(String name, Integer status, Integer pageNum, Integer pageSize) {
        //1.设置分页条件
        Page<NursingPlan> page = new Page<>(pageNum, pageSize);

        //2.设置业务条件
        LambdaQueryWrapper<NursingPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),NursingPlan::getPlanName,name)
                .eq(status != null,NursingPlan::getStatus,status);

        //3.执行查询
        page = getBaseMapper().selectPage(page,wrapper);

        page.getRecords().forEach(e->{
            User user = userMapper.selectById(e.getCreateBy());
            if (user != null){

                e.setCreator(user.getRealName());
            }

        });

        return new PageResponse<>(page);
    }
    //一个方法向数据库中发送了多条增删改语句，注意事务问题
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveNursingPlan(NursingPlanDto nursingPlanDto) {
        //从Dto中获取PlanName封装到NursingPlan对象中，保存到数据库
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto,NursingPlan.class);
        //默认值：启用
        nursingPlan.setStatus(1);
        //mp技术再新增后会自动执行主键返回
        getBaseMapper().insert(nursingPlan);
        //从dto中获取跟项目相关的计划信息，循环保存到中间表中
        nursingPlanDto.getProjectPlans().stream().forEach(nursingProjectPlan -> {
            //设置id
            nursingProjectPlan.setPlanId(nursingPlan.getId());
            //执行程序
            nursingProjectPlanMapper.insert(nursingProjectPlan);
        });
    }


    //主键查询
    @Override
    public NursingPlanVo findById(Long id) {
        NursingPlan nursingPlan = getBaseMapper().selectById(id);
        NursingPlanVo nursingPlanVo = BeanUtil.copyProperties(nursingPlan, NursingPlanVo.class);

        List<NursingProjectPlanVo>  list = nursingProjectPlanMapper.findByIdList(id);

        nursingPlanVo.setProjectPlans(list);

        return nursingPlanVo;
    }

    //修改
    @Override
    public void updateNursingPlan(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = BeanUtil.copyProperties(nursingPlanDto, NursingPlan.class);
        getBaseMapper().updateById(this.nursingPlan);

        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProjectPlan::getPlanId,nursingPlanDto.getId());
        nursingProjectPlanMapper.delete(wrapper);

        //从dto中获取跟项目相关的计划信息，循环保存到中间表中
        nursingPlanDto.getProjectPlans().stream().forEach(e->{
            e.setPlanId(nursingPlan.getId());
            nursingProjectPlanMapper.insert(e);
        });
    }

    @Override
    public void delete(Long id) {
        //先删除中间表中的
        LambdaQueryWrapper<NursingProjectPlan> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(NursingProjectPlan::getPlanId,id);
        nursingProjectPlanMapper.delete(wrapper);

        //再删除主表
        getBaseMapper().deleteById(id);
    }

    //启用禁用
    @Override
    public void switchStatus(Long id, Integer status) {
        nursingPlan.setId(id);
        nursingPlan.setStatus(status);
        nursingPlanMapper.updateById(nursingPlan);
    }



}
